$(function () {
    //筛选类别的动态渲染
    $.ajax({
        url: News.category_list,
        headers: { Authorization: localStorage.getItem('get_big_token') },
        success: function (res) {
            let html = template('stausTemp', res)
            $('.stauslist').html(html)
        }
    })
    let sbs = window.parent.frames.document.querySelector('#homeif')
    console.log($(sbs));
    $('#release_btn').on('click', function () {
        let sb1 = window.parent.frames.document.querySelectorAll('#homeif')
        let sb2 = window.parent.frames.document.querySelectorAll('#first')
        $(sb1).addClass('active')
        $(sb2).removeClass('active')
    })

    let Currentpage = 1
    let totalPage = 0
    //文章列表的动态渲染
    function Getarticle(type, state, perpage = 10) {
        $.ajax({
            url: News.article_query,
            data: { type, state, perpage, page: Currentpage },
            headers: { Authorization: localStorage.getItem('get_big_token') },
            success: function (res) {
                let data = res.data.data
                let html = template('listsTemp', data)
                $('.mp20 tbody').html(html)
                setPage(res.data.totalPage)
                totalPage = res.data.totalCount
            }
        })
    }

    //页码插件的使用
    function setPage(pageSum) {
        $(".pagination").bootstrapPaginator({
            //设置版本号
            bootstrapMajorVersion: 3,
            // 显示第几页
            currentPage: Currentpage,
            numberOfPages: 10,
            // 总页数
            totalPages: pageSum || 1,
            //当单击操作按钮的时候, 执行该函数, 调用ajax渲染页面
            onPageClicked: function (event, originalEvent, type, page) {
                Currentpage = page
                Getarticle($('#selCategory').val(), $('#selStatus').val(), $('.currentOpt').val())
            }
        })
    }

    //选择每页几条的下拉框的改变事件
    $('.currentOpt').on('change', function () {
        let value = $(this).val() - 0
        Currentpage = 1
        Getarticle($('#selCategory').val(), $('#selStatus').val(), value)
    })


    //筛选按钮的点击事件
    $('#btnSearch').on('click', function () {
        Currentpage = 1
        Getarticle($('#selCategory').val(), $('#selStatus').val())
    })

    //删除单个文章事件
    //父元素.on(事件类型,'dom元素','回调函数')
    $('tbody').on('click', 'a', function () {
        let id = $(this).data('id')
        //删除事件
        if ($(this).text() == '删除') {
            if (confirm('是否删除')) {
                $.ajax({
                    type: 'post',
                    url: News.article_delete,
                    headers: { Authorization: localStorage.getItem('get_big_token') },
                    data: { id },
                    success: function (res) {
                        alert(res.msg)
                        //判断总文章个数,除余每页显示的文章数
                        //假设:221除余10余1 的话 那就是最后一个,那就跳到前面一页
                        if (Currentpage > 1 && totalPage % 10 == 1) {
                            Currentpage--
                        }
                        Getarticle($('#selCategory').val(), $('#selStatus').val())
                    }
                })
            }
        }
        //编辑事件
        if ($(this).text() == '编辑') {
            //根据ID获取文章类别给编辑器里的value赋值
            location.href = './article_release.html?' + 'id=' + id

        }
    })

    Getarticle()
})